package com.yinduo.yongyou.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import com.yinduo.yongyou.R;
import com.yinduo.yongyou.entity.Phone;
import com.yinduo.yongyou.listener.PhoneClickedListener;

import java.util.ArrayList;

public class PhoneAdapter extends RecyclerView.Adapter<PhoneAdapter.ViewHolder> {
    private ArrayList<Phone> phone;
    public static PhoneClickedListener mPhoneClickedListener;
    private Context mContext;
    int index;

    public PhoneAdapter(Context context, ArrayList<Phone> phone, PhoneClickedListener phoneClickedListener) {
        this.mContext = context;
        this.phone = phone;
        this.mPhoneClickedListener = phoneClickedListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.setting_add_phone_item, viewGroup, false);
        ViewHolder vh = new ViewHolder(view);
        //将创建的View注册点击事件

        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, int position) {
        viewHolder.tv_phone.setText(phone.get(position).getPhoneNumb());
        final String mobile = viewHolder.tv_phone.getText().toString();
        if ("0".equals(phone.get(position).getIsDef())) {
            viewHolder.phone_check.setChecked(true);
//            setMySelection(position);
        } else {
            viewHolder.phone_check.setChecked(false);
        }
        viewHolder.phone_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mPhoneClickedListener.onPhoneClick(mobile);
            }
        });
//        if (position == index) {
//            // 判断当前position是否为选中项
//            viewHolder.phone_check.setChecked(false);
//        } else {
//            viewHolder.phone_check.setChecked(true);
//        }
    }


    public void setMySelection(int index) {
        this.index = index;
        notifyDataSetChanged();
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return phone.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_phone;
        public RadioButton phone_check;

        public ViewHolder(View view) {
            super(view);
            phone_check = (RadioButton) view.findViewById(R.id.phone_check);
            tv_phone = (TextView) view.findViewById(R.id.tv_phone);

        }
    }

}
